package com.sys.gradepaysys.config;

import com.sys.gradepaysys.model.Admin;
import com.sys.gradepaysys.model.Student;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 登录认证成功处理器类
 */
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //获取当登录用户信息
        Student student= (Student) httpServletRequest.getSession().getAttribute("user");
        Admin admin= (Admin) httpServletRequest.getSession().getAttribute("admin");
        Cookie cookie = null;
        if (student != null) {
            cookie = new Cookie("sid", String.valueOf(student.getsId()));
            httpServletRequest.getRequestDispatcher("/toProgram").forward(httpServletRequest, httpServletResponse);

        }else {
            cookie = new Cookie("aid", String.valueOf(admin.getAid()));
            httpServletRequest.getRequestDispatcher("/toAddEvent").forward(httpServletRequest, httpServletResponse);

        }
        httpServletResponse.addCookie(cookie);
        
    }
}
